package com.wanshi.controller;

import com.github.pagehelper.PageInfo;
import com.wanshi.po.Page;
import com.wanshi.po.RecordPo;
import com.wanshi.po.User;
import com.wanshi.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("dati")
public class RecordController {

    @Autowired
    private RecordService recordService;


    @RequestMapping("/myHistory")
    public String myHistory(Model model,HttpSession session,int pagenum) {
        User user = (User)session.getAttribute("user");
        Page<RecordPo> page = recordService.list(pagenum, user.getId());
        int total = recordService.getTotalDegreeAllTi();
        int fen = recordService.getTotalDegreeByUser(user.getId());

        model.addAttribute("total",total);
        model.addAttribute("page",page);
        model.addAttribute("fen",fen);
        return "listRecord";
    }

    @RequestMapping("/findRecordByClsName")
    public String findRecordByClsName(Model model,HttpSession session,int pagenum) {
        User user = (User)session.getAttribute("user");
        Page<RecordPo> page = recordService.findRecordByClsName(pagenum, user.getClsname());
        model.addAttribute("page",page);
        return "listClsRecord";
    }

    @RequestMapping("/findTrueByUserId")
    public String findTrueByUserId(Model model,HttpSession session,int result,int pagenum) {
        User user = (User)session.getAttribute("user");
        Page<RecordPo> page = recordService.listByResult(user.getId(), result, pagenum);
        int total = recordService.getTotalDegreeAllTi();
        int fen = recordService.getTotalDegreeByUser(user.getId());

        model.addAttribute("total",total);
        model.addAttribute("page",page);
        model.addAttribute("fen",fen);
        return "listRecordByResult";
    }

    @RequestMapping("/delRecord")
    @ResponseBody
    public String delRecord(HttpSession session,int id) {
        User user = (User)session.getAttribute("user");
        recordService.delRecord(user.getId(),id);
        return "true";
    }



}
